package com.meilai.project.controller.web.business.customer;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.entity.business.customer.CustomerInfo;
import com.meilai.project.service.business.customer.CustomerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * <p>
 * 客户信息表 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2022-01-06
 */
@Api(tags = "客户")
@RestController
@RequestMapping("api/web/customerInfo")
public class CustomerInfoController {

    @Autowired
    private CustomerInfoService service;

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public CommonResult page(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @RequestParam(name = "is_my_customer_page") Boolean is_my_customer_page,
                             @RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "industry_id", required = false) Long industry_id,
                             @RequestParam(name = "maintenance_staff_id", required = false) Long maintenance_staff_id) {
        return new CommonResult().success(service.page(page, page_size, name, industry_id, is_my_customer_page, null, maintenance_staff_id));
    }

    @GetMapping("/allPage")
    @ApiOperation("分页查询，显示所有客户")
    public CommonResult allPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                                @RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "industry_id", required = false) Long industry_id,
                                @RequestParam(name = "follow_up") Boolean follow_up,
                                @RequestParam(name = "deptId", required = false) Long deptId) {
        return new CommonResult().success(service.allPage(page, page_size, name, industry_id, follow_up, deptId));
    }

    @GetMapping("list-all")
    @ApiOperation("查询当前人跟进的所有客户")
    public ApiResult<CustomerInfo> listAll() {
        return ApiResult.ok(service.listAll());
    }

    @GetMapping("company-list-all")
    @ApiOperation("查询公司的所有客户")
    public ApiResult<CustomerInfo> companyListAll() {
        return ApiResult.ok(service.companyListAll());
    }

    @GetMapping("/one")
    @ApiOperation("根据ID获取客户信息")
    public CommonResult one(@RequestParam(name = "id") Long id) {
        return new CommonResult().success(service.getOneDtoById(id));
    }

    @PostMapping("/edit")
    @ApiOperation("指派销售")
    public CommonResult edit(@RequestParam(name = "id") Long id, @RequestParam(name = "maintenance_staff_id") Long maintenance_staff_id) {
        return new CommonResult().success(service.edit(id, maintenance_staff_id));
    }


    @GetMapping("/advanceList")
    @ApiOperation("推送动作集合")
    public CommonResult advanceList() {
        return new CommonResult().success(service.advanceList());
    }

    @PostMapping("/addNextVisitDate")
    @ApiOperation("添加下次跟进时间")
    public CommonResult addNextVisitDate(@RequestParam("id") Long id,
                                         @RequestParam("date")
                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
                                         @RequestParam("purpose") String purpose) {
        return new CommonResult().success(service.update(Wrappers.<CustomerInfo>lambdaUpdate().set(CustomerInfo::getNext_visit_time, date).set(CustomerInfo::getPurpose, purpose).eq(CustomerInfo::getId, id)));
    }

    @GetMapping("/visitRecordList")
    @ApiOperation("根据客户ID获取拜访记录")
    public CommonResult visitRecordList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "page_size", defaultValue = "10") Integer page_size,
                                        @RequestParam("customer_id") Long customer_id,
                                        @RequestParam(value = "contact_name", required = false) String contact_name,
                                        @RequestParam(value = "begin_date", required = false) @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin_date,
                                        @RequestParam(value = "end_date", required = false) @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end_date) {
        return new CommonResult().success(service.visitRecordList(page, page_size, customer_id, contact_name, begin_date, end_date));
    }


    @GetMapping("/companyAdvanceList")
    @ApiOperation("推送动作集合")
    public CommonResult companyAdvanceList(@Param("year") Integer year, @Param("month") Integer month) {
        return new CommonResult().success(service.companyAdvanceList(year, month, null));
    }

    @GetMapping("/getContactById")
    @ApiOperation("获取联系人")
    public CommonResult getContactById(@RequestParam("id") Long id) {
        return new CommonResult().success(service.getContactById(id));
    }

    @GetMapping("/visitCommentsPage")
    @ApiOperation("历史批注信息")
    public CommonResult visitCommentsPage(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
            @RequestParam("customer_id") Long customer_id,
            @RequestParam(value = "comments", required = false) String comments) {
        return new CommonResult().success(service.visitCommentsPage(page, page_size, customer_id, comments));
    }
}

